package com.wangzh.concurrent.lock;

import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

//
public class ReentranLockTest {
    static  ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) {



        Thread t1Started = new Thread(() -> {
            reentrantLock.lock();
            try {
                System.out.println("t1 started");
                TimeUnit.SECONDS.sleep(5);
                System.out.println("t1 end");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally {
                reentrantLock.unlock();
            }

        });


        Thread t2Started = new Thread(() -> {
            System.out.println("t2 started");
            while (reentrantLock.isLocked()) {

            }
            System.out.println("t2 end");
        });
        t2Started.start();
        t1Started.start();
    }
}
